ExceptionAlgo : Fix translatePythonException()
reference counting bug
#1397
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
PyErr_Fetch()
documentation says "you own a reference to each object retrieved", which means we need to decrement the reference count before returning, as we have no interest in sharing ownership. We were doing this via theboost::python::handle<>
objects informatInternal()
, but since d0731a3 we haven't been callingformatInternal()
for exceptions bound viaIECorePython::ExceptionClass
. That meant we were leaking such Python exceptions such that they would never be destroyed.The solution, and the moral of the story, is to always hold
PyObject *
via a sensible RAII class likehandle
orobject
, and to always do that as early as possible.You might be forgiven for thinking that leaking a little exception object isn't that big a deal. But Python exceptions have a
__traceback__
attribute that contains the entire stack at the point the exception was raised, and that contains all the local variables from all of those stack frames. So the leak can actually include a completely arbitrary amount of stuff.